#!/bin/bash

db_name=tradedata

dir=/root/$db_name
backup_dir=$dir/backup

if [ ! -d $backup_dir ];
then
mkdir $backup_dir
fi

sql_name=$db_name-$(date -d today +%Y-%m-%d)
filepath=$backup_dir/$sql_name

yesterday_sql_name=$db_name-$(date -d yesterday +%Y-%m-%d)
yesterday_filepath=$backup_dir/$yesterday_sql_name


if [ -f $filepath.sql.tar.bz2 ];
then
echo 删除已有当天的存档 ...
rm -f $filepath*
fi

mysqldump -u root -p801111 $db_name --skip-tz-utc > $filepath.sql && 
sed -i "1 i USE $db_name;" $filepath.sql &&
tar -cf $filepath.sql.tar $filepath.sql && bzip2 $filepath.sql.tar  

rm -f $filepath.sql 

rm -f $yesterday_filepath.sql.tar.bz2

